package com.freecloud.rpc.demo1.client;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Author: maomao
 * @Date: 2021/9/1 10:02 上午
 */
public class NewIoClient {

    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("localhost", 8080));
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put("client message".getBytes());
            //反转
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
            int i = socketChannel.read(byteBuffer);
            if (i <= 0) {
                System.out.println("没有收到数据");
            } else {
                System.out.println("收到了数据：" + new String(byteBuffer.array()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
